1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gtk.Dialog; 26 27 private import glib.ConstructionException; 28 private import glib.Str; 29 private import gobject.ObjectG; 30 private import gobject.Signals; 31 private import gtk.Box; 32 private import gtk.HeaderBar; 33 private import gtk.Widget; 34 private import gtk.Window; 35 private import gtk.c.functions; 36 public import gtk.c.types; 37 private import std.algorithm; 38 39 40 /** 41 * Dialogs are a convenient way to prompt the user for a small amount 42 * of input. 43 * 44 *  45 * 46 * Typical uses are to display a message, ask a question, or anything else 47 * that does not require extensive effort on the user’s part. 48 * 49 * The main area of a `GtkDialog` is called the "content area", and is yours 50 * to populate with widgets such a `GtkLabel` or `GtkEntry`, to present 51 * your information, questions, or tasks to the user. 52 * 53 * In addition, dialogs allow you to add "action widgets". Most commonly, 54 * action widgets are buttons. Depending on the platform, action widgets may 55 * be presented in the header bar at the top of the window, or at the bottom 56 * of the window. To add action widgets, create your `GtkDialog` using 57 * [ctor@Gtk.Dialog.new_with_buttons], or use 58 * [method@Gtk.Dialog.add_button], [method@Gtk.Dialog.add_buttons], 59 * or [method@Gtk.Dialog.add_action_widget]. 60 * 61 * `GtkDialogs` uses some heuristics to decide whether to add a close 62 * button to the window decorations. If any of the action buttons use 63 * the response ID %GTK_RESPONSE_CLOSE or %GTK_RESPONSE_CANCEL, the 64 * close button is omitted. 65 * 66 * Clicking a button that was added as an action widget will emit the 67 * [signal@Gtk.Dialog::response] signal with a response ID that you specified. 68 * GTK will never assign a meaning to positive response IDs; these are 69 * entirely user-defined. But for convenience, you can use the response 70 * IDs in the [enum@Gtk.ResponseType] enumeration (these all have values 71 * less than zero). If a dialog receives a delete event, the 72 * [signal@Gtk.Dialog::response] signal will be emitted with the 73 * %GTK_RESPONSE_DELETE_EVENT response ID. 74 * 75 * Dialogs are created with a call to [ctor@Gtk.Dialog.new] or 76 * [ctor@Gtk.Dialog.new_with_buttons]. The latter is recommended; it allows 77 * you to set the dialog title, some convenient flags, and add buttons. 78 * 79 * A “modal” dialog (that is, one which freezes the rest of the application 80 * from user input), can be created by calling [method@Gtk.Window.set_modal] 81 * on the dialog. When using [ctor@Gtk.Dialog.new_with_buttons], you can also 82 * pass the %GTK_DIALOG_MODAL flag to make a dialog modal. 83 * 84 * For the simple dialog in the following example, a [class@Gtk.MessageDialog] 85 * would save some effort. But you’d need to create the dialog contents manually 86 * if you had more than a simple message in the dialog. 87 * 88 * An example for simple `GtkDialog` usage: 89 * 90 * ```c 91 * // Function to open a dialog box with a message 92 * void 93 * quick_message (GtkWindow *parent, char *message) 94 * { 95 * GtkWidget *dialog, *label, *content_area; 96 * GtkDialogFlags flags; 97 * 98 * // Create the widgets 99 * flags = GTK_DIALOG_DESTROY_WITH_PARENT; 100 * dialog = gtk_dialog_new_with_buttons ("Message", 101 * parent, 102 * flags, 103 * _("_OK"), 104 * GTK_RESPONSE_NONE, 105 * NULL); 106 * content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); 107 * label = gtk_label_new (message); 108 * 109 * // Ensure that the dialog box is destroyed when the user responds 110 * 111 * g_signal_connect_swapped (dialog, 112 * "response", 113 * G_CALLBACK (gtk_window_destroy), 114 * dialog); 115 * 116 * // Add the label, and show everything we’ve added 117 * 118 * gtk_box_append (GTK_BOX (content_area), label); 119 * gtk_widget_show (dialog); 120 * } 121 * ``` 122 * 123 * # GtkDialog as GtkBuildable 124 * 125 * The `GtkDialog` implementation of the `GtkBuildable` interface exposes the 126 * @content_area as an internal child with the name “content_area”. 127 * 128 * `GtkDialog` supports a custom `<action-widgets>` element, which can contain 129 * multiple `<action-widget>` elements. The “response” attribute specifies a 130 * numeric response, and the content of the element is the id of widget 131 * (which should be a child of the dialogs @action_area). To mark a response 132 * as default, set the “default” attribute of the `<action-widget>` element 133 * to true. 134 * 135 * `GtkDialog` supports adding action widgets by specifying “action” as 136 * the “type” attribute of a `<child>` element. The widget will be added 137 * either to the action area or the headerbar of the dialog, depending 138 * on the “use-header-bar” property. The response id has to be associated 139 * with the action widget using the `<action-widgets>` element. 140 * 141 * An example of a `GtkDialog` UI definition fragment: 142 * 143 * ```xml 144 * <object class="GtkDialog" id="dialog1"> 145 * <child type="action"> 146 * <object class="GtkButton" id="button_cancel"/> 147 * </child> 148 * <child type="action"> 149 * <object class="GtkButton" id="button_ok"> 150 * </object> 151 * </child> 152 * <action-widgets> 153 * <action-widget response="cancel">button_cancel</action-widget> 154 * <action-widget response="ok" default="true">button_ok</action-widget> 155 * </action-widgets> 156 * </object> 157 * ``` 158 * 159 * # Accessibility 160 * 161 * `GtkDialog` uses the %GTK_ACCESSIBLE_ROLE_DIALOG role. 162 */ 163 public class Dialog : Window 164 { 165 /** the main Gtk struct */ 166 protected GtkDialog* gtkDialog; 167 168 /** Get the main Gtk struct */ 169 public GtkDialog* getDialogStruct(bool transferOwnership = false) 170 { 171 if (transferOwnership) 172 ownedRef = false; 173 return gtkDialog; 174 } 175 176 /** the main Gtk struct as a void* */ 177 protected override void* getStruct() 178 { 179 return cast(void*)gtkDialog; 180 } 181 182 /** 183 * Sets our main struct and passes it to the parent class. 184 */ 185 public this (GtkDialog* gtkDialog, bool ownedRef = false) 186 { 187 this.gtkDialog = gtkDialog; 188 super(cast(GtkWindow*)gtkDialog, ownedRef); 189 } 190 191 /** */ 192 public void addButtons(string[] buttonsText, ResponseType[] responses) 193 { 194 for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++) 195 { 196 addButton(buttonsText[i], responses[i]); 197 } 198 } 199 200 /** 201 */ 202 203 /** */ 204 public static GType getType() 205 { 206 return gtk_dialog_get_type(); 207 } 208 209 /** 210 * Creates a new dialog box. 211 * 212 * Widgets should not be packed into the `GtkWindow` 213 * directly, but into the @content_area and @action_area, 214 * as described above. 215 * 216 * Returns: the new dialog as a `GtkWidget` 217 * 218 * Throws: ConstructionException GTK+ fails to create the object. 219 */ 220 public this() 221 { 222 auto __p = gtk_dialog_new(); 223 224 if(__p is null) 225 { 226 throw new ConstructionException("null returned by new"); 227 } 228 229 this(cast(GtkDialog*) __p); 230 } 231 232 /** 233 * Adds an activatable widget to the action area of a `GtkDialog`. 234 * 235 * GTK connects a signal handler that will emit the 236 * [signal@Gtk.Dialog::response] signal on the dialog when the widget 237 * is activated. The widget is appended to the end of the dialog’s action 238 * area. 239 * 240 * If you want to add a non-activatable widget, simply pack it into 241 * the @action_area field of the `GtkDialog` struct. 242 * 243 * Params: 244 * child = an activatable widget 245 * responseId = response ID for @child 246 */ 247 public void addActionWidget(Widget child, int responseId) 248 { 249 gtk_dialog_add_action_widget(gtkDialog, (child is null) ? null : child.getWidgetStruct(), responseId); 250 } 251 252 /** 253 * Adds a button with the given text. 254 * 255 * GTK arranges things so that clicking the button will emit the 256 * [signal@Gtk.Dialog::response] signal with the given @response_id. 257 * The button is appended to the end of the dialog’s action area. 258 * The button widget is returned, but usually you don’t need it. 259 * 260 * Params: 261 * buttonText = text of button 262 * responseId = response ID for the button 263 * 264 * Returns: the `GtkButton` widget that was added 265 */ 266 public Widget addButton(string buttonText, int responseId) 267 { 268 auto __p = gtk_dialog_add_button(gtkDialog, Str.toStringz(buttonText), responseId); 269 270 if(__p is null) 271 { 272 return null; 273 } 274 275 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) __p); 276 } 277 278 /** 279 * Returns the content area of @dialog. 280 * 281 * Returns: the content area `GtkBox`. 282 */ 283 public Box getContentArea() 284 { 285 auto __p = gtk_dialog_get_content_area(gtkDialog); 286 287 if(__p is null) 288 { 289 return null; 290 } 291 292 return ObjectG.getDObject!(Box)(cast(GtkBox*) __p); 293 } 294 295 /** 296 * Returns the header bar of @dialog. 297 * 298 * Note that the headerbar is only used by the dialog if the 299 * [property@Gtk.Dialog:use-header-bar] property is %TRUE. 300 * 301 * Returns: the header bar 302 */ 303 public HeaderBar getHeaderBar() 304 { 305 auto __p = gtk_dialog_get_header_bar(gtkDialog); 306 307 if(__p is null) 308 { 309 return null; 310 } 311 312 return ObjectG.getDObject!(HeaderBar)(cast(GtkHeaderBar*) __p); 313 } 314 315 /** 316 * Gets the response id of a widget in the action area 317 * of a dialog. 318 * 319 * Params: 320 * widget = a widget in the action area of @dialog 321 * 322 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE 323 * if @widget doesn’t have a response id set. 324 */ 325 public int getResponseForWidget(Widget widget) 326 { 327 return gtk_dialog_get_response_for_widget(gtkDialog, (widget is null) ? null : widget.getWidgetStruct()); 328 } 329 330 /** 331 * Gets the widget button that uses the given response ID in the action area 332 * of a dialog. 333 * 334 * Params: 335 * responseId = the response ID used by the @dialog widget 336 * 337 * Returns: the @widget button that uses the given 338 * @response_id 339 */ 340 public Widget getWidgetForResponse(int responseId) 341 { 342 auto __p = gtk_dialog_get_widget_for_response(gtkDialog, responseId); 343 344 if(__p is null) 345 { 346 return null; 347 } 348 349 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) __p); 350 } 351 352 /** 353 * Emits the ::response signal with the given response ID. 354 * 355 * Used to indicate that the user has responded to the dialog in some way. 356 * 357 * Params: 358 * responseId = response ID 359 */ 360 public void response(int responseId) 361 { 362 gtk_dialog_response(gtkDialog, responseId); 363 } 364 365 /** 366 * Sets the default widget for the dialog based on the response ID. 367 * 368 * Pressing “Enter” normally activates the default widget. 369 * 370 * Params: 371 * responseId = a response ID 372 */ 373 public void setDefaultResponse(int responseId) 374 { 375 gtk_dialog_set_default_response(gtkDialog, responseId); 376 } 377 378 /** 379 * A convenient way to sensitize/desensitize dialog buttons. 380 * 381 * Calls `gtk_widget_set_sensitive (widget, @setting)` 382 * for each widget in the dialog’s action area with the given @response_id. 383 * 384 * Params: 385 * responseId = a response ID 386 * setting = %TRUE for sensitive 387 */ 388 public void setResponseSensitive(int responseId, bool setting) 389 { 390 gtk_dialog_set_response_sensitive(gtkDialog, responseId, setting); 391 } 392 393 /** 394 * Emitted when the user uses a keybinding to close the dialog. 395 * 396 * This is a [keybinding signal](class.SignalAction.html). 397 * 398 * The default binding for this signal is the Escape key. 399 */ 400 gulong addOnClose(void delegate(Dialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 401 { 402 return Signals.connect(this, "close", dlg, connectFlags ^ ConnectFlags.SWAPPED); 403 } 404 405 /** 406 * Emitted when an action widget is clicked. 407 * 408 * The signal is also emitted when the dialog receives a 409 * delete event, and when [method@Gtk.Dialog.response] is called. 410 * On a delete event, the response ID is %GTK_RESPONSE_DELETE_EVENT. 411 * Otherwise, it depends on which action widget was clicked. 412 * 413 * Params: 414 * responseId = the response ID 415 */ 416 gulong addOnResponse(void delegate(int, Dialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 417 { 418 return Signals.connect(this, "response", dlg, connectFlags ^ ConnectFlags.SWAPPED); 419 } 420 }